<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use app\common\serve\Plat;

/**
 * 财务
 * Class Finance
 * @package app\admin\controller
 */
class Finance extends Admin
{

    /**
     * 平台账户
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->request->param();

        $list = model('platMoneyLog')->where([])->order('id DESC')
            ->paginate(15, false, array(
                'param' => $param
            ));

        $data = array(
            'list' => $list,
            'page' => $list->render(),
            'param' => $param
        );

        $this->assign($data);

        $this->assign('money', db('plat_money')->find(['id' => 1]));

        $this->setMeta('平台账户及流水');
        return $this->fetch();
    }

    /**
     * 平台账户变更流水
     * @return mixed
     * @throws \think\exception\DbException
     */
//    public function change()
//    {
//        $param = $this->request->param();
//        $map = [];
//
//        $list = model('platMoneyLog')->where($map)->order('id DESC')
//            ->paginate(15, false, array(
//                'param' => $param
//            ));
//
//        $data = array(
//            'list' => $list,
//            'page' => $list->render(),
//            'param' => $param
//        );
//
//        $this->assign($data);
//
//        $this->setMeta('平台账户变更流水');
//        return $this->fetch();
//    }

    /**
     * 平台充值
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function recharge()
    {
        if ($this->request->isPost()) {

            $params = $this->request->param();
            $result = Plat::recharge([
                'id' => 1,
                'money' => $params['money']
            ]);

            if (is_array($result) && $result['code'] == 1) {
                $this->success('操作成功', 'index');
            } else {
                $this->error('操作失败，请联系开发者！');
            }
        }

        if (session('user_auth.uid') != config('user_administrator')) $this->error('管理员才可以充值');

        $this->assign('plat', db('plat_money')->find(['id' => 1]));
        $this->setMeta('平台充值');
        return $this->fetch();
    }


}